package com.leantech.android.browser.browser.web

import android.content.pm.ActivityInfo
import android.view.View
import android.view.ViewGroup
import android.webkit.WebChromeClient
import android.widget.FrameLayout
import androidx.fragment.app.FragmentActivity

class FullScreenSupport(private val activity: FragmentActivity) {

    private var customView: View? = null
    private var originOrientation:Int = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
    private var originSystemUiVisibility: Int = -1
    private var customViewCallback: WebChromeClient.CustomViewCallback? = null

    fun onShowCustomView(view: View, callback: WebChromeClient.CustomViewCallback) {
        if(customView != null){
            onHideCustomView()
            return
        }
        customView = view
        val decorView = activity.window.decorView as FrameLayout
        originSystemUiVisibility = decorView.systemUiVisibility
        originOrientation = activity.requestedOrientation
        customViewCallback = callback
        decorView.addView(view, FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
        decorView.systemUiVisibility =
            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_IMMERSIVE or View.SYSTEM_UI_FLAG_LAYOUT_STABLE

    }

    fun onHideCustomView() {
        val decorView = activity.window.decorView as FrameLayout
        decorView.removeView(customView)
        customView = null
        decorView.systemUiVisibility = originSystemUiVisibility
        activity.requestedOrientation = originOrientation
        customViewCallback?.onCustomViewHidden()
        customViewCallback = null
    }

}